//
//  VisitorInfoCell.m
//  ppkc2016
//
//  Created by Laura on 2017/3/20.
//  Copyright © 2017年 拼凑的断音. All rights reserved.
//

#import "VisitorInfoCell.h"
#import "XXTool.h"
#import "LiuyanViewController.h"

@interface VisitorInfoCell ()

@property(nonatomic) UIView* view;
@property(nonatomic) UIImageView* head;
@property(nonatomic) UILabel* name;
@property(nonatomic) UILabel* position;
@property(nonatomic) UILabel* company;
@property(nonatomic) UIButton* liuyan;
@end


@implementation VisitorInfoCell

-(UIView*)view{
    if(_view == nil){
        _view=[[UIView alloc]init];
        _view.backgroundColor=[UIColor whiteColor];
        [self addSubview:_view];
    }
    return _view;
}

-(UIImageView*) head{
    if(_head == nil){
        _head=[[UIImageView alloc]init];
        _head.contentMode = UIViewContentModeScaleAspectFill;
        _head.clipsToBounds = YES;
        [self.view addSubview:_head];
    }
    return _head;
}

-(UILabel*) name{
    if(_name == nil){
        _name=[[UILabel alloc] init];
        _name.textColor=[XXTool colorWithHexString:@"000000"];
        _name.font=[XXTool boldSystemFontOfSize:15];
        _name.textAlignment=NSTextAlignmentLeft;
        _name.numberOfLines=0;
        [self.view addSubview:_name];
    }
    return _name;
}

-(UILabel*) position{
    if(_position == nil){
        _position=[[UILabel alloc] init];
        _position.textColor=[UIColor grayColor];
        _position.font=[XXTool systemFontOfSize:15];
        _position.textAlignment=NSTextAlignmentLeft;
        _position.numberOfLines=0;
        [self.view addSubview:_position];
    }
    return _position;
}

-(UILabel*) company{
    if(_company == nil){
        _company=[[UILabel alloc]init];
        _company.textColor=[UIColor grayColor];
        _company.font=[XXTool systemFontOfSize:15];
        _company.textAlignment=NSTextAlignmentLeft;
        _company.numberOfLines=0;
        [self.view addSubview:_company];
    }
    return _company;
}

-(UIButton*) liuyan{
    if(_liuyan == nil){
        _liuyan=[[UIButton alloc] init];
        [_liuyan setTitle:NSLocalizedString(@"留言", nil) forState:UIControlStateNormal];
        [_liuyan setTitleColor:[XXTool colorWithProject] forState:UIControlStateNormal];
        _liuyan.layer.borderWidth = 0.6f;
        _liuyan.layer.cornerRadius = 5;
        _liuyan.layer.borderColor = [XXTool colorWithProject].CGColor;
        [self.view addSubview:_liuyan];
    }
    return _liuyan;
}

-(void)initUI{
    self.backgroundColor=[UIColor clearColor];
    [self view];
    [self head];
    [self name];
    [self position];
    [self company];
    [self liuyan];
    
}

/**
 * 设置 位置
 */
-(void)refreshLayout:(CGRect)frame{
    
    _view.frame=CGRectMake(6, 6, frame.size.width-6*2, frame.size.height-6*2);
    self.head.frame=CGRectMake(10, 16, 60, 60);
    self.head.layer.cornerRadius = self.head.xx_height/2;
    self.name.frame=CGRectMake(_head.frame.size.width+_head.frame.origin.x+10
                               , _head.frame.origin.y - 4
                               ,_view.xx_width - 80 - 75 - 10 - 5
                               ,16);
    self.position.frame=CGRectMake(_name.xx_origin.x,
                                   _name.xx_bottom + 8
                                   ,self.name.xx_width
                                   , self.name.xx_height);
    self.company.frame=CGRectMake(self.name.xx_origin.x,
                                  self.position.xx_bottom+8
                                  ,self.name.xx_width
                                  ,self.name.xx_height);
    self.liuyan.frame=CGRectMake(_position.frame.size.width+_position.frame.origin.x+10, (_view.xx_height - 30)/2, 75, 30);
}

-(void)setUserHeadImg:(NSString *)imgUrl compltion:(void (^)(UIImage *))block
{
    [UIImageView downloadImageWithUrl:imgUrl
                           Completion:^(UIImage *imageData) {
                               if (imageData!=nil)
                               {
                                   [_head setImage:imageData];
                                   if (block)
                                   {
                                       block(_head.image);
                                   }
                               }
                               else
                               {
                                   [_head setImage:[UIImage imageNamed:@"默认头像"]];
                               }
                           }];
}

-(void)setCellWithData:(VisitorInfoModel*)model{
   [_head setImageWithUrl:model.userphotourl placerHolder:[UIImage imageNamed:@"默认头像"]];
    _head.clipsToBounds=YES;
    _head.layer.cornerRadius=_head.frame.size.height/2;
    _name.text=model.username;
    _position.text=model.userposition;
    _company.text=model.cpname;
    [_liuyan setTag:[model.orderid intValue]];
    [_liuyan addTarget:self action:@selector(clickLiuyan:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)clickLiuyan:(id)sender{
    // 获取ID
    NSInteger tag=[sender tag];
    NSLog(@"%ld",tag);
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dropLiuyan" object:nil userInfo:@{@"tag":[NSNumber numberWithInteger:tag]}];
    
}

@end
